package com.part2.action.function;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.part2.Constants;
import com.part2.action.common.CommonAction;
import com.part2.action.common.Password;
import com.part2.action.common.RandomGUID;
import com.part2.action.common.form.CustomActionForm;
import com.part2.action.common.form.ValueObject;
import com.part2.action.common.validator.Validator;
import com.part2.beans.Users;
import com.part2.business.function.UsersService;
import com.part2.common.query.Page;

public class UsersAction extends CommonAction<Users> {
	private final Log logger = LogFactory.getLog(getClass());
	private UsersService usersService;

	public void setUserService(UsersService usersService) {
		this.usersService = usersService;
	}

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("::::::::::User-List");
		Page<Users> p = initPage(request, Constants.PRE_PAGE_SIZE);// 设置每页的记录数
		Page<Users> page = usersService.findUsers(p);
		if (page != null) {
			request.setAttribute("page", page);

		} else {
			request.setAttribute("res", "暂无记录！");
		}
		logger.info("::::::::::User-List End");
		return mapping.findForward("list");
	}

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("::::::::::User-Add");
		ValueObject uVo = ((CustomActionForm) form).getVoa().getValue("users");
		// String id = new RandomGUID()+"-users";
		String id = uVo.getField("name");
		String userName = uVo.getField("name");
		String password = Password.createPassword(uVo.getField("password"));
		String purview = uVo.getField("purview");

		Users u = new Users();
		u.setUserId(id);
		u.setUserName(userName);
		u.setPassword(password);
		u.setPurview(Short.parseShort(purview));
		u.setCreateDate(new Date());
		usersService.addUsers(u);
		logger.info("::::::::::User-Add End");
		return this.list(mapping, form, request, response);
	}

	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("::::::::::User-Edit");
		ValueObject uVo = ((CustomActionForm) form).getVoa().getValue("users");
		String id = uVo.getField("id");
		if (Validator.isNotNull(id)) {
			Users u = usersService.findUsersByPrimeKey(id);
			if (u != null) {
				u.setUserName(uVo.getField("name"));
				if (Validator.isNotNull(uVo.getField("password").trim())) {
					if (u.getPassword().equals(uVo.getField("password").trim()))
						;
					else
						u.setPassword(Password.createPassword(uVo
								.getField("password")));
				}
				u.setPurview(Short.parseShort(uVo.getField("purview")));
				u.setLastModifiedDate(new Date());
				usersService.updateUsers(u);
				logger.info("::::::::::User-Edit End");
				return this.list(mapping, form, request, response);
			}
		}
		return null;
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("::::::::::User-Delete");
		String id = request.getParameter("id");
		if (Validator.isNotNull(id)) {
			usersService.deleteUsers(id);
			return list(mapping, form, request, response);
		}
		logger.info("::::::::::User-Delete End");
		return null;
	}

}
